package com.jeevandeshmukh.glidetoastlib;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;

import java.util.Optional;

public class GlideToast {

    private String message;
    private Context context;
    private int icon;
    public static int LENGTHTOOLONG = 1500;
    public static int LENGTHLONG = 1000;
    public static int LENGTHMEDIUM = 750;
    public static int LENGTHSHORT = 500;
    public static int LENGTHQUICK = 250;
    public static int TOP = 1;
    public static int CENTER = 2;
    public static int BOTTOM = 3;
    public static String DEFAULTTOAST = "DEFAULT";
    public static String SUCCESSTOAST = "SUCCESS";
    public static String FAILTOAST = "FAIL";
    public static String WARNINGTOAST = "WARN";
    public static String INFOTOAST = "INFO";
    public static String CUSTOMTOAST = "CUSTOM";
    public static String ICONTYPE_GRAPHIC_VECTOR = "vector";
    public static String ICONTYPE_PNG_JPG = "png/jpg";


    private GlideToast() {
    }

    public static class makeToast {
        private String message;
        private Context context;
        private int icon;
        private String iconType;
        private int duration;
        private int gravity;
        private String style;
        private RgbColor backgroundcolor;


        public makeToast(Context context, String message, int duration, String style, int gravity) {
            this.context = context;
            this.message = message;
            this.duration = duration;
            this.style = style;
            this.gravity = gravity;

        }

        public makeToast(Context context, String message, int duration, String style) {
            this.context = context;
            this.message = message;
            this.duration = duration;
            this.style = style;

        }

        public makeToast(Context context, String message, int duration, String style, int gravity, int icon, String iconType, RgbColor backgroundcolor) {
            this.context = context;
            this.message = message;
            this.duration = duration;
            this.style = style;
            this.icon = icon;
            this.iconType = iconType;
            this.backgroundcolor = backgroundcolor;
            this.gravity = gravity;

        }

        public void setStyle(String style, DependentLayout background, Image iconImg) {
            switch (style) {
                case "SUCCESS":
                    ShapeElement successShapeElement = new ShapeElement();
                    successShapeElement.setRgbColor(new RgbColor(56, 142, 60));
                    background.setBackground(successShapeElement);
                    iconImg.setBackground(ElementScatter.getInstance(context).parse(ResourceTable.Graphic_ic_check_black_24dp));
                    break;
                case "FAIL":
                    ShapeElement failShapeElement = new ShapeElement();
                    failShapeElement.setRgbColor(new RgbColor(211, 47, 47));
                    background.setBackground(failShapeElement);
                    iconImg.setBackground(ElementScatter.getInstance(context).parse(ResourceTable.Graphic_ic_clear_black_24dp));
                    break;
                case "WARN":
                    ShapeElement warnShapeElement = new ShapeElement();
                    warnShapeElement.setRgbColor(new RgbColor(245, 124, 0));
                    background.setBackground(warnShapeElement);
                    iconImg.setBackground(ElementScatter.getInstance(context).parse(ResourceTable.Graphic_ic_pan_tool_black_24dp));
                    break;
                case "INFO":
                    ShapeElement infoShapeElement = new ShapeElement();
                    infoShapeElement.setRgbColor(new RgbColor(48, 63, 159));
                    background.setBackground(infoShapeElement);
                    iconImg.setBackground(ElementScatter.getInstance(context).parse(ResourceTable.Graphic_ic_info_outline_black_24dp));
                    break;
                case "CUSTOM":
                    if (backgroundcolor != null) {
                        ShapeElement shapeElement = new ShapeElement();
                        shapeElement.setRgbColor(backgroundcolor);
                        background.setBackground(shapeElement);
                        try {
                            if (iconType.equals(ICONTYPE_GRAPHIC_VECTOR)) {
                                iconImg.setBackground(ElementScatter.getInstance(context).parse(icon));
                            } else {
                                PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
                                initializationOptions.size = new Size(60, 60);
                                initializationOptions.pixelFormat = PixelFormat.ARGB_8888;
                                Optional<PixelMap> pixelMapOriginal = ResTUtil.getPixelMap(context, icon);
                                PixelMap newResult = PixelMap.create(pixelMapOriginal.get(), initializationOptions);
                                iconImg.setPixelMap(newResult);
                            }
                        } catch (Exception e) {
                            ToastDialog toastDialog = new ToastDialog(context);
                            toastDialog.setText("icon and icontype mismatch").setDuration(1000).show();
                        }
                    }
                    break;
                default:
                    ShapeElement defaultShapeElement = new ShapeElement();
                    defaultShapeElement.setRgbColor(new RgbColor(43, 42, 42));
                    background.setBackground(defaultShapeElement);
                    iconImg.setBackground(ElementScatter.getInstance(context).parse(ResourceTable.Graphic_ic_info_outline_black_24dp));
                    break;

            }
        }

        public void setGravity(int gravity, CommonDialog dialog) {
            switch (gravity) {
                case 1:
                    dialog.setAlignment(LayoutAlignment.TOP);
                    break;
                case 2:
                    dialog.setAlignment(LayoutAlignment.CENTER);
                    break;
                case 3:
                    dialog.setAlignment(LayoutAlignment.BOTTOM);
                    break;
                default:
                    dialog.setAlignment(LayoutAlignment.BOTTOM);
                    break;
            }
        }


        Text messageTV;
        Image iconImg;
        Dialog dialog;
        DependentLayout background;

        private final Runnable mUpdater = new Runnable() {
            @Override
            public void run() {
                dialog.hide();
            }
        };

        public void show() {
            dialog = new Dialog(context);
            ComponentContainer componentContainer = (ComponentContainer) LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_toast, null, false);
            dialog.initAnimatorGroup(componentContainer);
            messageTV = (Text) componentContainer.findComponentById(ResourceTable.Id_toast_text);
            iconImg = (Image) componentContainer.findComponentById(ResourceTable.Id_toast_icon);
            background = (DependentLayout) componentContainer.findComponentById(ResourceTable.Id_background);
            messageTV.setText(message);
            setStyle(style, background, iconImg);
            dialog.setSize(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
            setGravity(gravity, dialog);
            dialog.setContentCustomComponent(componentContainer);
            dialog.setTransparent(true);
            dialog.show();
            TaskDispatcher dispatcher = context.getUITaskDispatcher();
            dispatcher.delayDispatch(mUpdater, duration);
        }
    }
}

